package csu.web.mypetstore.web.servlet;
//参考CatalogAcitonBean
//点击大类的图片，进入product展示

import csu.web.mypetstore.domain.Category;
import csu.web.mypetstore.domain.Product;
import csu.web.mypetstore.service.CatalogService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "CategoryFormServlet", urlPatterns = {"/categoryForm"})
public class CategoryFormServlet extends HttpServlet {
    private CatalogService catalogService;
    private static final String CATEGORY_FORM = "/WEB-INF/jsp/catalog/category.jsp";

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //category.jsp中${sessionScope.category.name}、${sessionScope.productList}要查
        //获取category的信息
        String categoryId = req.getParameter("categoryId");//获取点击的大类图片的id main.jsp中点击相应图片会带过去CategoryId
        catalogService = new CatalogService();
        Category category = catalogService.getCategory(categoryId);
        //利用categoryId寻找productList
        List<Product> productList = catalogService.getProductListByCategory(categoryId);
        //展示信息
        HttpSession session = req.getSession();
        session.setAttribute("category", category);//名字匹配上category.jsp的<h2>${sessionScope.category.name}</h2>,下面同理
        session.setAttribute("productList", productList);

        session.setAttribute("action","viewingCategoryForm");

        req.getRequestDispatcher(CATEGORY_FORM).forward(req, resp);
    }
}
